home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 54655 / 54655.xpi / chrome / gathererPopup.jar / content / overlay.js < prev    next >
Text File  |  2009-12-15  |  5KB  |  130 lines

  1. with (gathererPopup) {
  2.  
  3. gathererPopup.overlay = {
  4.     ///////////////////////////////////////////////////////////////////
  5.     // Initialization
  6.     ///////////////////////////////////////////////////////////////////
  7.  
  8.     onLoad: function(e) {
  9.         var self = gathererPopup.overlay;
  10.  
  11.         window.addEventListener("mouseover", self.onMouseOver, false);
  12.     },
  13.  
  14.     onUnload: function(e) {
  15.         var self = gathererPopup.overlay;
  16.  
  17.         window.removeEventListener("load", self.onLoad, false);
  18.         window.removeEventListener("unload", self.onUnload, false);
  19.         window.removeEventListener("mouseover", self.onMouseOver, false);
  20.     },
  21.  
  22.     ///////////////////////////////////////////////////////////////////
  23.     // Events
  24.     ///////////////////////////////////////////////////////////////////
  25.  
  26.     onMouseOver: function(e) {
  27.         try {
  28.             var self = gathererPopup.overlay;
  29.  
  30.             if (e.target.ownerDocument instanceof HTMLDocument) {
  31.                 self.hidePopup(e.target.ownerDocument);
  32.                 if (e.target.localName.toLowerCase() == "a")
  33.                     self.hoverLink(e.target, e);
  34.             }
  35.         } catch (e) {
  36.             // if fails, ignore it
  37.             Cu.reportError(e);
  38.         }
  39.     },
  40.  
  41.     ///////////////////////////////////////////////////////////////////
  42.     // Logic
  43.     ///////////////////////////////////////////////////////////////////
  44.  
  45.     oldUrl: /javascript:autoCardWindow2?\(['"](.*?)['"]/i,
  46.     newUrl: "http://gatherer.wizards.com/Pages/Card/Details.aspx?name=",
  47.  
  48.     req: null,
  49.     imgRelativeUrl: /\/Image.ashx\?multiverseid=(\d+)/i,
  50.     imgAbsoluteUrl: "http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=###&type=card",
  51.     popupId: "firefox-gathererPopup",
  52.     cachePrefix: "firefox-gathererPopup-cache-",
  53.  
  54.     hoverLink: function(a, e) {
  55.         if (a.href.search(this.oldUrl) >= 0) {
  56.             // if link matches, replace it with real url for middleclicking
  57.             var name = a.href.match(this.oldUrl)[1]
  58.                         .replace(/_/g, " ")
  59.                         .replace(/\[/g, "'");
  60.             var url = this.newUrl + encodeURIComponent(name);
  61.             a.setAttribute("href", url);
  62.         }
  63.  
  64.         if (a.href.indexOf(this.newUrl) >= 0) {
  65.             // abort the previous request if user move mouse too fast
  66.             try {
  67.                 this.req.abort();
  68.                 this.req = null;
  69.             } catch (e) {
  70.                 // ignore errors
  71.             }
  72.  
  73.             // show popup and "Loading..." at mouse position
  74.             var popup = this.getPopup(a.ownerDocument);
  75.             popup.style.top = (e.pageY+10) + "px";
  76.             popup.style.left = (e.pageX+15) + "px";
  77.  
  78.             // get gatherer page and extract the image link
  79.             if (a.hasAttribute("multiverseurl")) {
  80.                 popup.style.background = "black url(" + a.getAttribute("multiverseurl")
  81.                                          + ") no-repeat -12px -12px";
  82.             }
  83.             else {
  84.                 this.req = get(a.href, (function(data) {
  85.                     try {
  86.                         if (!data) return;
  87.  
  88.                         var id = data.match(this.imgRelativeUrl)[1];
  89.                         var url = this.imgAbsoluteUrl.replace("###", id);
  90.                         popup.style.background = "black url(" + url + ") no-repeat -12px -12px";
  91.  
  92.                         a.setAttribute("multiverseurl", url);
  93.                     } catch (e) {
  94.                         // ignore errors
  95.                     }
  96.                 }).bind(this));
  97.             }
  98.         }
  99.     },
  100.  
  101.     getPopup: function getPopup(doc) {
  102.         // always get rid of the previous popup
  103.         // this cost us next to nothing, ensure stray styles won't persist
  104.         this.hidePopup(doc);
  105.  
  106.         // any styles we forget?
  107.         var popup = doc.createElement("div");
  108.         popup.setAttribute("id", this.popupId);
  109.         popup.setAttribute("style", "display: block; position: absolute; padding: 0; margin: 0;\
  110.                                      width: 198px; height: 283px; border: 1px solid #444;\
  111.                                      background: #555;\
  112.                                      -moz-box-shadow: black 2px 2px 10px;\
  113.                                      z-index: 999999;");
  114.         doc.body.appendChild(popup);
  115.  
  116.         return popup;
  117.     },
  118.  
  119.     hidePopup: function(doc) {
  120.         var popup = doc.getElementById(this.popupId);
  121.         if (popup) popup.parentNode.removeChild(popup);
  122.     },
  123. }
  124.  
  125. window.addEventListener("load", gathererPopup.overlay.onLoad, false);
  126. window.addEventListener("unload", gathererPopup.overlay.onUnload, false);
  127.  
  128. } // with
  129.  
  130.